<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/src/corelib/thread/qthreadstorage.cpp -->
<head>
  <title>Qt 4.3: QThreadStorage Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QThreadStorage Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1>
<p>The QThreadStorage class provides per-thread data storage. <a href="#details">More...</a></p>
<pre> #include &lt;QThreadStorage&gt;</pre><p><b>Note:</b> All the functions in this class are <a href="threads.html#thread-safe">thread-safe</a>.</p>
<ul>
<li><a href="qthreadstorage-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h3>Public Functions</h3>
<ul>
<li><div class="fn"/><b><a href="qthreadstorage.html#QThreadStorage">QThreadStorage</a></b> ()</li>
<li><div class="fn"/><b><a href="qthreadstorage.html#dtor.QThreadStorage">~QThreadStorage</a></b> ()</li>
<li><div class="fn"/>bool <b><a href="qthreadstorage.html#hasLocalData">hasLocalData</a></b> () const</li>
<li><div class="fn"/>T &amp; <b><a href="qthreadstorage.html#localData">localData</a></b> ()</li>
<li><div class="fn"/>T <b><a href="qthreadstorage.html#localData-2">localData</a></b> () const</li>
<li><div class="fn"/>void <b><a href="qthreadstorage.html#setLocalData">setLocalData</a></b> ( T <i>data</i> )</li>
</ul>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QThreadStorage class provides per-thread data storage.</p>
<p>QThreadStorage is a template class that provides per-thread data storage.</p>
<p><i>Note that due to compiler limitations, QThreadStorage can only store pointers.</i></p>
<p>The <a href="qthreadstorage.html#setLocalData">setLocalData</a>() function stores a single thread-specific value for the calling thread. The data can be accessed later using <a href="qthreadstorage.html#localData">localData</a>(). QThreadStorage takes ownership of the data (which must be created on the heap with <tt>new</tt>) and deletes it when the thread exits, either normally or via termination.</p>
<p>The <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>() function allows the programmer to determine if data has previously been set using the <a href="qthreadstorage.html#setLocalData">setLocalData</a>() function. This is also useful for lazy initializiation.</p>
<p>For example, the following code uses QThreadStorage to store a single cache for each thread that calls the cacheObject() and removeFromCache() functions. The cache is automatically deleted when the calling thread exits.</p>
<pre> QThreadStorage&lt;QCache&lt;QString, SomeClass&gt; *&gt; caches;

 void cacheObject(const QString &amp;key, SomeClass *object)
 {
     if (!caches.hasLocalData())
         caches.setLocalData(new QCache&lt;QString, SomeClass&gt;);

     caches.localData()-&gt;insert(key, object);
 }

 void removeFromCache(const QString &amp;key)
 {
     if (!caches.hasLocalData())
         return;

     caches.localData()-&gt;remove(key);
 }</pre>
<a name="caveats"></a>
<h3>Caveats</h3>
<ul>
<li>As noted above, QThreadStorage can only store pointers due to compiler limitations.</li>
<li>The QThreadStorage destructor does not delete per-thread data. QThreadStorage only deletes per-thread data when the thread exits or when <a href="qthreadstorage.html#setLocalData">setLocalData</a>() is called multiple times.</li>
<li>QThreadStorage can be used to store data for the <tt>main()</tt> thread. QThreadStorage deletes all data set for the <tt>main()</tt> thread when <a href="qapplication.html">QApplication</a> is destroyed, regardless of whether or not the <tt>main()</tt> thread has actually finished.</li>
</ul>
<p>See also <a href="qthread.html">QThread</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="QThreadStorage"></a>QThreadStorage::QThreadStorage ()</h3>
<p>Constructs a new per-thread data storage object.</p>
<h3 class="fn"><a name="dtor.QThreadStorage"></a>QThreadStorage::~QThreadStorage ()</h3>
<p>Destroys the per-thread data storage object.</p>
<p>Note: The per-thread data stored is not deleted. Any data left in <a href="qthreadstorage.html">QThreadStorage</a> is leaked. Make sure that all threads using <a href="qthreadstorage.html">QThreadStorage</a> have exited before deleting the <a href="qthreadstorage.html">QThreadStorage</a>.</p>
<p>See also <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p>
<h3 class="fn"><a name="hasLocalData"></a>bool QThreadStorage::hasLocalData () const</h3>
<p>Returns true if the calling thread has non-zero data available; otherwise returns false.</p>
<p>See also <a href="qthreadstorage.html#localData">localData</a>().</p>
<h3 class="fn"><a name="localData"></a>T &amp; QThreadStorage::localData ()</h3>
<p>Returns a reference to the data that was set by the calling thread.</p>
<p>Note: <a href="qthreadstorage.html">QThreadStorage</a> can only store pointers. This function returns a reference to the pointer that was set by the calling thread. The value of this reference is 0 if no data was set by the calling thread,</p>
<p>See also <a href="qthreadstorage.html#setLocalData">setLocalData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p>
<h3 class="fn"><a name="localData-2"></a>T QThreadStorage::localData () const</h3>
<p>This is an overloaded member function, provided for convenience.</p>
<p>Returns a copy of the data that was set by the calling thread.</p>
<p>Note: <a href="qthreadstorage.html">QThreadStorage</a> can only store pointers. This function returns a pointer to the data that was set by the calling thread. If no data was set by the calling thread, this function returns 0.</p>
<p>See also <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p>
<h3 class="fn"><a name="setLocalData"></a>void QThreadStorage::setLocalData ( T <i>data</i> )</h3>
<p>Sets the local data for the calling thread to <i>data</i>. It can be accessed later using the <a href="qthreadstorage.html#localData">localData</a>() functions.</p>
<p>If <i>data</i> is 0, this function deletes the previous data (if any) and returns immediately.</p>
<p>If <i>data</i> is non-zero, <a href="qthreadstorage.html">QThreadStorage</a> takes ownership of the <i>data</i> and deletes it automatically either when the thread exits (either normally or via termination) or when setLocalData() is called again.</p>
<p>Note: <a href="qthreadstorage.html">QThreadStorage</a> can only store pointers. The <i>data</i> argument must be either a pointer to an object created on the heap (i.e&#x2e; using <tt>new</tt>) or 0. You should not delete <i>data</i> yourself; <a href="qthreadstorage.html">QThreadStorage</a> takes ownership and will delete the <i>data</i> itself.</p>
<p>See also <a href="qthreadstorage.html#localData">localData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
